package no.ntnu.fp.mmi;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;
import javax.swing.ListSelectionModel;
import javax.swing.WindowConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import no.ntnu.fp.su.Event;
import no.ntnu.fp.su.Message;
import no.ntnu.fp.su.Platform;
import no.ntnu.fp.su.Status;
import no.ntnu.fp.su.WatchWell;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
@SuppressWarnings("serial")
public class MasterWatchPanel extends javax.swing.JFrame implements
		PropertyChangeListener {
	private JList platformList;
	private JList wellList;
	private JList recentEventsList;
	private JList messagesList;
	private JLabel choosePlatformLabel;
	private JLabel recentEventsLabel;
	private JLabel messagesLabel;
	private JButton goToPlatformButton;
	private JLabel masterWatchLabel;
	// private JButton editWellButton;
	private JLabel wellListLabel;
	private JLabel platformListLabel;
	Font font = new Font("Candera", Font.BOLD, 16);

	private MasterWatchModel model;
	// private WatchModel wModel;
	private JScrollPane jScrollPane1;
	private JScrollPane jScrollPane3;
	private JButton clearAllEventsButton;
	private JButton clearEventButton;
	private JScrollPane jScrollPane2;
	private JButton openMessageButton;
	private DefaultListModel wellListModel;
	private DefaultListModel recentEventsListModel;
	private DefaultListModel messagesListModel;
	private DefaultListModel platformListModel;
	{
		// Set Look & Feel
		try {
			javax.swing.UIManager
					.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Constructor
	 * 
	 * @param {@link mwm} model this panel uses to draw
	 * 
	 */
	public MasterWatchPanel(final MasterWatchModel mwm) {
		super();
		this.model = mwm;
		this.setLocationRelativeTo(null);
		this.setVisible(true);
		this.setTitle("Master Watch");
		messagesListModel = new DefaultListModel();
		try {
			setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
			getContentPane().setLayout(null);
			setSize(800, 700);

			{
				platformListModel = new DefaultListModel();
				platformList = new JList(platformListModel);
				getContentPane().add(platformList);
				for (int i = 0; i < mwm.getPlatformList().size(); i++) {
					platformListModel.add(i, mwm.getPlatformList().get(i));
				}
				platformList.setBounds(62, 139, 149, 286);
				platformList.setModel(platformListModel);
				platformList
						.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
				platformList
						.addListSelectionListener(new ListSelectionListener() {
							public void valueChanged(ListSelectionEvent e) {
								if (!platformList.isSelectionEmpty()) {
									wellList.setVisible(true);
									wellListModel.removeAllElements();
									mwm
											.setActivePlatform(mwm
													.getPlatformList()
													.get(
															platformList
																	.getSelectedIndex()));
									for (int i = 0; i < mwm.getActivePlatform()
											.getWells().size(); i++) {
										wellListModel.add(i, mwm
												.getActivePlatform().getWells()
												.get(i));
									}
									goToPlatformButton.setEnabled(true);
									wellListLabel.setEnabled(true);
									choosePlatformLabel.setVisible(false);

								}

							}
						});
				platformList.setCellRenderer(new ListCellRenderer() {
					protected ImageIcon createImageIcon(String path,
							String description) {
						java.net.URL imgURL = getClass().getResource(path);
						if (imgURL != null) {
							return new ImageIcon(imgURL, description);
						} else {
							System.err.println("Couldn't find file: " + path);
						}
						return null;
					}

					public boolean isRed(Platform platt) {
						boolean isRed = false;
						for (int i = 0; i < platt.getWells().size(); i++) {
							if (platt.getWells().get(i).getStatus() == Status.RED) {
								isRed = true;
							}
						}

						return isRed;
					}

					public boolean isYellow(Platform platt) {
						boolean isYellow = false;
						for (int i = 0; i < platt.getWells().size(); i++) {
							if (platt.getWells().get(i).getStatus() == Status.YELLOW) {
								isYellow = true;
							}
						}

						return isYellow;
					}

					public Status getStatus(Platform platt) {
						if (isRed(platt)) {
							return Status.RED;
						}
						if (isYellow(platt)) {
							return Status.YELLOW;
						}

						return Status.GREEN;

					}

					public Component getListCellRendererComponent(JList list,
							Object value, int index, boolean isSelected,
							boolean cellHasFocus) {
						Platform platt = (Platform) value;
						JLabel label = new JLabel(platt.getName());
						ImageIcon ikon;
						list.setBackground(Color.LIGHT_GRAY);
						label.setForeground(Color.black);

						switch (getStatus(platt)) {
						case GREEN:
							ikon = createImageIcon("status_green.png", "GREEN");
							label.setIcon(ikon);
							break;
						case RED:
							ikon = createImageIcon("status_red.png", "RED");
							label.setIcon(ikon);
							break;
						case YELLOW:
							ikon = createImageIcon("status_yellow.png",
									"YELLOW");
							label.setIcon(ikon);
							break;
						}
						if (isSelected) {
							label.setForeground(Color.white);
						}
						label.setBackground(Color.black);
						refreshMSG();
						return label;
					}
				});

			}
			{
				jScrollPane3 = new JScrollPane();
				getContentPane().add(jScrollPane3);
				jScrollPane3.setBounds(218, 138, 183, 286);
				{
					wellListModel = new DefaultListModel();
					wellList = new JList(wellListModel);
					jScrollPane3.setViewportView(wellList);
					wellList.setModel(wellListModel);
					wellList.setBounds(611, 82, 184, 288);
					wellList
							.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
					wellList.setVisible(false);
					wellList.setCellRenderer(new ListCellRenderer() {
						protected ImageIcon createImageIcon(String path,
								String description) {
							java.net.URL imgURL = getClass().getResource(path);
							if (imgURL != null) {
								return new ImageIcon(imgURL, description);
							} else {
								System.err.println("Couldn't find file: "
										+ path);
							}
							return null;
						}

						public Component getListCellRendererComponent(
								JList list, Object value, int index,
								boolean isSelected, boolean cellHasFocus) {
							WatchWell well = (WatchWell) value;
							JLabel label = new JLabel(well.getName());
							ImageIcon ikon;
							list.setBackground(Color.LIGHT_GRAY);
							label.setForeground(Color.black);
							// Font f = new Font("Dialog", Font.PLAIN, 20);
							if (isSelected) {
								label.setForeground(Color.white);
							}

							switch (well.getStatus()) {
							case GREEN:
								ikon = createImageIcon("status_green.png",
										"GREEN");
								label.setIcon(ikon);
								break;
							case RED:
								ikon = createImageIcon("status_red.png",
										"GREEN");
								label.setIcon(ikon);
								break;
							case YELLOW:
								ikon = createImageIcon("status_yellow.png",
										"GREEN");
								label.setIcon(ikon);
								break;
							}
							label.setBackground(Color.black);
							return label;
						}
					});

				}
			}

			{
				platformListLabel = new JLabel();
				platformListLabel.setFont(font);
				getContentPane().add(platformListLabel);
				platformListLabel.setText("Platform List");
				platformListLabel.setBounds(62, 116, 104, 20);
			}
			{

				ImageIcon image = new ImageIcon(getClass().getResource(
						"recent.png"));
				JLabel innhold = new JLabel();
				getContentPane().add(innhold);
				innhold.setIcon(image);
				innhold.setBounds(531, 87, 48, 48);
				setVisible(true);

			}
			{
				ImageIcon wellImage = new ImageIcon(getClass().getResource(
						"message.png"));
				JLabel inside1 = new JLabel();
				getContentPane().add(inside1);
				inside1.setIcon(wellImage);
				inside1.setBounds(64, 454, 36, 43);
				setVisible(true);

			}
			{
				ImageIcon logo = new ImageIcon(getClass().getResource(
						"logo.jpg"));
				JLabel logos = new JLabel();
				getContentPane().add(logos);
				logos.setIcon(logo);
				logos.setBounds(352, 471, 426, 186);
				setVisible(true);

			}
			{
				recentEventsLabel = new JLabel();
				recentEventsLabel.setFont(font);
				getContentPane().add(recentEventsLabel);
				recentEventsLabel.setText("Recent Events");
				recentEventsLabel.setBounds(585, 116, 133, 19);
			}
			{
				wellListLabel = new JLabel();
				wellListLabel.setFont(font);
				getContentPane().add(wellListLabel);
				wellListLabel.setText("Well List");
				wellListLabel.setBounds(218, 117, 91, 17);
				wellListLabel.setEnabled(false);
			}
			{
				messagesLabel = new JLabel();
				messagesLabel.setFont(font);
				getContentPane().add(messagesLabel);
				messagesLabel.setText("Messages");
				messagesLabel.setBounds(110, 471, 101, 26);
			}

			{
				masterWatchLabel = new JLabel();
				Font f = new Font("Candera", Font.BOLD, 23);
				masterWatchLabel.setFont(f);
				getContentPane().add(masterWatchLabel);
				masterWatchLabel.setText("Master Watch");
				masterWatchLabel.setBounds(202, 37, 164, 31);
			}
			{
				goToPlatformButton = new JButton();
				getContentPane().add(goToPlatformButton);
				goToPlatformButton.setText("Go to Platform");
				goToPlatformButton.setBounds(62, 427, 113, 24);
				goToPlatformButton.setEnabled(false);
				goToPlatformButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						int wModel = platformList.getSelectedIndex();
						mwm.getMaster().gotoPlatform(
								mwm.getPlatformList().get(wModel));
					}
				});
			}
			{
				choosePlatformLabel = new JLabel();
				choosePlatformLabel.setFont(new Font("Candera", Font.BOLD, 23));
				getContentPane().add(choosePlatformLabel);
				choosePlatformLabel.setText("(Choose a platform)");
				choosePlatformLabel.setBounds(365, 32, 220, 41);
			}
			{
				jScrollPane2 = new JScrollPane();
				getContentPane().add(jScrollPane2);
				jScrollPane2.setBounds(62, 497, 256, 97);
				{

					messagesListModel = new DefaultListModel();
					messagesList = new JList(messagesListModel);
					jScrollPane2.setViewportView(messagesList);
					messagesList.setModel(messagesListModel);
					messagesList.setBounds(611, 402, 287, 223);
					messagesList.setBackground(Color.LIGHT_GRAY);
					messagesList.setPreferredSize(new java.awt.Dimension(222,
							95));
					for (int i = 0; i < mwm.getMessageList().size(); i++) {
						messagesListModel.add(i, mwm.getMessageList().get(i));
					}
					messagesList
							.addListSelectionListener(new messageListSelection());

					messagesList.setCellRenderer(new ListCellRenderer() {
						public Component getListCellRendererComponent(
								JList list, Object value, int index,
								boolean isSelected, boolean cellHasFocus) {
							JLabel label = new JLabel();
							Message mess = (Message) value;
							String name = mess.getFrom().getName();
							label.setText(name + "   " + mess.getTimestamp());
							if (isSelected) {
								label.setForeground(Color.white);
							}
							return label;
						}
					});
				}
			}
			{
				openMessageButton = new JButton();
				getContentPane().add(openMessageButton);
				openMessageButton.setText("Open Message");
				openMessageButton.setBounds(62, 600, 120, 28);
				openMessageButton.setEnabled(false);
				openMessageButton.addActionListener(new openMessageEvent());
			}
			{
				clearEventButton = new JButton();
				getContentPane().add(clearEventButton);
				clearEventButton.setText("Clear");
				clearEventButton.setBounds(536, 432, 71, 28);
				clearEventButton.setEnabled(false);
				clearEventButton.addActionListener(new clearEventEvent());
			}
			{
				clearAllEventsButton = new JButton();
				getContentPane().add(clearAllEventsButton);
				clearAllEventsButton.setText("Clear All");
				clearAllEventsButton.setBounds(644, 432, 74, 28);
				clearAllEventsButton
						.addActionListener(new clearAllEventsEvent());
			}
			{
				jScrollPane1 = new JScrollPane();
				getContentPane().add(jScrollPane1);
				jScrollPane1.setBounds(500, 142, 278, 279);
				{
					recentEventsListModel = new DefaultListModel();
					recentEventsList = new JList(recentEventsListModel);
					jScrollPane1.setViewportView(recentEventsList);
					recentEventsList.setModel(recentEventsListModel);
					recentEventsList.setBounds(500, 142, 278, 279);
					recentEventsList.setBackground(Color.lightGray);
					for (int i = 0; i < mwm.getEventList().getEvents().size(); i++) {
						recentEventsListModel.add(0, mwm.getEventList()
								.getEvents().get(i));
					}
					recentEventsList.setCellRenderer(new ListCellRenderer() {
						public Component getListCellRendererComponent(
								JList arg0, Object arg1, int arg2,
								boolean arg3, boolean arg4) {
							Event event = (Event) arg1;
							JLabel label = new JLabel(event.getContent()
									+ "    " + event.getTime());
							if (arg3) {
								label.setBackground(Color.black);
								label.setForeground(Color.white);
							}
							return label;
						}
					});
					recentEventsList
							.addListSelectionListener(new ListSelectionListener() {
								public void valueChanged(ListSelectionEvent e) {
									if (!recentEventsList.isSelectionEmpty()) {
										clearEventButton.setEnabled(true);
									}

								}
							});

				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void setModel(MasterWatchModel MWmodel) {
		this.model = MWmodel;
		model.addPropertyChangeListener(this);
	}

	@Override
	public void propertyChange(PropertyChangeEvent evt) {

	}

	/**
	 * Opens a viewMessagePanel
	 * 
	 * 
	 * 
	 */
	public class openMessageEvent implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			new ViewMessagePanel(
					(Message) messagesListModel.getElementAt(messagesList
							.getSelectedIndex()));
		}

	}

	public class messageListSelection implements ListSelectionListener {
		public void valueChanged(ListSelectionEvent e) {
			if (!messagesList.isSelectionEmpty()) {
				openMessageButton.setEnabled(true);
				model.setActiveMessage((Message) messagesList
						.getSelectedValue());
			}

		}
	}

	/**
	 * Clears events from eventlist
	 * 
	 * 
	 * 
	 */
	public class clearEventEvent implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			int[] selected = recentEventsList.getSelectedIndices();
			int counter = 0;
			for (int i = 0; i < selected.length; i++) {
				recentEventsListModel.removeElementAt(selected[i - counter]);
				counter++;
			}
		}

	}

	/**
	 * 
	 * Clears ALL events from eventlist
	 * 
	 */
	public class clearAllEventsEvent implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			recentEventsListModel.removeAllElements();
		}

	}

	public void refreshMSG() {
		this.messagesListModel.clear();
		for (int i = 0; i < this.model.getMessageList().size(); i++) {
			this.messagesListModel.add(0, this.model.getMessageList().get(i));
		}
	}

}
